﻿using AutoMapper;
using B.S.BasicData.API.Read.Application.Command.Leave;
using B.S.BasicData.API.Read.DTOS.Leave;
using B.S.BasicData.Domain.Entities.Leave;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Collections.Generic;

namespace B.S.BasicData.API.Read.Application.Handler.Leave
{
    /// <summary>
    /// 获取请假类别请求命令处理类
    /// </summary>
    public class GetLeaveTypeHandler:IRequestHandler<GetLeaveTypeCommand,ApiResult<List<LeaveTypeDTO>>>
    {
        IBaseRepository<LeaveTypeModel> leaveRep;
        IMapper mapper;

        public GetLeaveTypeHandler(IBaseRepository<LeaveTypeModel> leaveRep, IMapper mapper)
        {
            this.leaveRep = leaveRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<LeaveTypeDTO>>> Handle(GetLeaveTypeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<LeaveTypeDTO>> res = new ApiResult<List<LeaveTypeDTO>>();

                var leavetypes=leaveRep.GetAll();
                var model=mapper.Map<List<LeaveTypeDTO>>(leavetypes);

                res.Msg = "获取申请类别";
                res.Data = model;
                res.Code = ApiEnum.Success;
                return Task.FromResult(res);


            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
